﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using Module.EntitiesInfo;

namespace Module.DataAccess
{
	public class CMSGalleryDB
	{
		#region ConnectionString
        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        public CMSGalleryDB()
        {
            ConnectionString = ConfigurationManager.ConnectionStrings["localsql"].ToString();
        }
        #endregion

		#region Methods
		
		public virtual int Insert(CMSGalleryInfo cMSGalleryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Title", cMSGalleryInfo.Title),
				new SqlParameter("@Teaser", cMSGalleryInfo.Teaser),
				new SqlParameter("@Status", cMSGalleryInfo.Status),
				new SqlParameter("@ShowType", cMSGalleryInfo.ShowType),
				new SqlParameter("@HitCount", cMSGalleryInfo.HitCount),
				new SqlParameter("@CreatedOn", cMSGalleryInfo.CreatedOn),
				new SqlParameter("@CreatedBy", cMSGalleryInfo.CreatedBy),
				new SqlParameter("@Avatar", cMSGalleryInfo.Avatar),
				new SqlParameter("@BigAvatar", cMSGalleryInfo.BigAvatar),
				new SqlParameter("@ZoneId", cMSGalleryInfo.ZoneId)
			};

			return (int) SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "CMS_Gallery_Insert", parameters);
		}
		
		public virtual void Update(CMSGalleryInfo cMSGalleryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@GalleryId", cMSGalleryInfo.GalleryId),
				new SqlParameter("@Title", cMSGalleryInfo.Title),
				new SqlParameter("@Teaser", cMSGalleryInfo.Teaser),
				new SqlParameter("@Status", cMSGalleryInfo.Status),
				new SqlParameter("@ShowType", cMSGalleryInfo.ShowType),
				new SqlParameter("@HitCount", cMSGalleryInfo.HitCount),
				new SqlParameter("@CreatedOn", cMSGalleryInfo.CreatedOn),
				new SqlParameter("@CreatedBy", cMSGalleryInfo.CreatedBy),
				new SqlParameter("@Avatar", cMSGalleryInfo.Avatar),
				new SqlParameter("@BigAvatar", cMSGalleryInfo.BigAvatar),
				new SqlParameter("@ZoneId", cMSGalleryInfo.ZoneId)
			};

			SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "CMS_Gallery_Update", parameters);
		}
	
		public virtual void Delete(int galleryId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@GalleryId", galleryId)
			};

			SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "CMS_Gallery_Delete", parameters);
		}


        public virtual DataTable GetAll(int param, int type)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Param", param),	
			     new SqlParameter("@Type", type)
			};
            return SqlHelper.ExecuteDataset(ConnectionString, CommandType.StoredProcedure, "CMS_Gallery_GetAll", parameters).Tables[0];
        }

        public virtual IDataReader GetInfo(int id)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@GalleryId", id)				
			};
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "CMS_Gallery_GetInfo", parameters);

        }


        public IDataReader Select(int? type, string para_1, string para_2)
        {
            try
            {
                return SqlHelper.ExecuteReader(ConnectionString, "CMS_Gallery_Select", type, para_1, para_2);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public DataTable SelectTbl(int? type, string para_1, string para_2)
        {
            try
            {
                return SqlHelper.ExecuteDataset(ConnectionString, "CMS_Gallery_Select", type, para_1, para_2).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
        }
          /// <summary>
        /// Tính tổng số bản ghi
        /// </summary>
        /// <param name="keysearch">Từ khóa tìm kiếm</param>
        /// <param name="categoryId">Thể loại câu hỏi</param>
        /// <param name="starDatetime">Ngày bắt đầu</param>
        /// <param name="endDatetime">Ngày kết thúc</param>
        /// <param name="beginRow">Bản ghi đầu tiền</param>
        /// <param name="endRow">Bản ghi cuối</param>
        /// <returns></returns>
        public virtual int GetCount(string keysearch, int zoneId,int status, DateTime starDatetime, DateTime endDatetime, int beginRow, int endRow)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{                          
               new SqlParameter("@keysearch", keysearch),  
               new SqlParameter("@ZoneId",zoneId),   
               new SqlParameter("@Status",status),   
               new SqlParameter("@starDatetime", starDatetime),
               new SqlParameter("@endDatetime", endDatetime),
               new SqlParameter("@IsTotalRow", 1)
			};

            return (int)SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "CMS_Gallery_Search", parameters);
        }

        /// <summary>
        /// Tìm kiếm
        /// </summary>
        /// <param name="keysearch">Từ khóa tìm kiếm</param>
        /// <param name="categoryId">Thể loại câu hỏi</param>
        /// <param name="starDatetime">Ngày bắt đầu</param>
        /// <param name="endDatetime">Ngày kết thúc</param>
        /// <param name="beginRow">Bản ghi đầu</param>
        /// <param name="endRow">Bản ghi cuối</param>
        /// <param name="orderByColumn">Sắp xếp</param>
        /// <returns></returns>
        public virtual DataTable Search(string keysearch, int zoneId,int status, DateTime starDatetime, DateTime endDatetime, int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{                
                new SqlParameter("@Keysearch", keysearch),  
                new SqlParameter("@ZoneId",zoneId), 
                new SqlParameter("@Status",status), 
                new SqlParameter("@StarDatetime", starDatetime),
                new SqlParameter("@EndDatetime", endDatetime),               
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@IsTotalRow", 0)
			};

            return SqlHelper.ExecuteDataset(ConnectionString, CommandType.StoredProcedure, "CMS_Gallery_Search", parameters).Tables[0];
        }

        //Update Trạng thái 
        public virtual void UpdateStatus(int galleryId, int status, int param)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@GalleryId", galleryId),
                new SqlParameter("@Status", status),
                new SqlParameter("@Param", param),
			};

            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "CMS_Gallery_UpdateStatus", parameters);
        }

		#endregion
	}
}
